import Vue from 'vue'
/*
 * @parm url string  短url
 * @parm pram Object 传入参数
 * @parm ShowLoading  boolen 请请求是否显示loading
 * @parm Methods  string 请求协议默认为get
 * @pram ShowMsg 请求完成是否弹出提示信息
 */
let baseurl = '';
let isShow=false//ture未未正式，false未测试

function requestMethods(url,data,Methods,config){
	if(isShow){
		if(config.new){
			baseurl='https://apiorder.yjspchina.com'
		}
		uni.setStorageSync('upimage','https://apisystem.yjspchina.com/api/v1/Authority/UploadImage')
	}else {
		if(config.new){
			baseurl='https://zl.yjspchina.com'
		}else if(config.system){
			baseurl='https://system.yjspchina.com'
		}else if(config.store){
			baseurl='https://store.yjspchina.com'
		}
		uni.setStorageSync('image','https://system.yjspchina.com')
		uni.setStorageSync('upimage','https://system.yjspchina.com/api/v1/Authority/UploadImage')
	}
	
	if (config.ShowLoading) {
		uni.showLoading({
			title: '请稍候...',
			mask:true
		})
	}
	if((Methods == 'POST'||Methods == 'PUT'||Methods == 'DELETE')&&!config.new){
	}
	return new Promise(function(resolve, reject) {
		uni.request({
			url: baseurl+url,
			data: data,
			header: {
				'custom-header': 'Access-Control-Allow-Origin:*',
				'contentType': 'application/json; charset=utf-8',
				'Authorization':uni.getStorageSync('productToken'),
				'AccessToken':uni.getStorageSync('token')
				// 'x-cos-cood':config.yan||''
			},
			method: Methods,
			success: (res) => {
				if (config.ShowLoading) {
					uni.hideLoading();
				}
				if(res.header['access-code']){
					let dataA={
						verify:res.header['access-code'],//后端返回的验证码在响应头中，为了不影响其他接口的返回因此需要进行判断
						...res.data
					}
					resolve(dataA)
				}
				if (res.statusCode == 200) {
					if(config.getAllData){//不需要判断success true或false
						resolve(res.data);
					}else{
						if(res.data.status){
							resolve(res.data);
						}else if( res.data.code == 402||res.data.code == 17 ){
							if(!res.data.status){
								uni.clearStorageSync();
								uni.reLaunch({
									url:'/pages/home/login/login'
								})
							}
						}else if(config.ShowMsg&&(res.data.msg||res.data.Msg)){
								uni.showToast({
									title: res.data.msg||res.data.Msg,
									icon: 'none'
								})
							}
						else{
							uni.showToast({
								title: res.data.message||res.data.msg,
								icon: 'none'
							})
						}
						reject(res);
					}
				} else if (res.statusCode == 404) {
					if (config.ShowMsg) {
						uni.showToast({
							title: '访问的资源不存在',
							icon: 'none'
						})
					}
				} else if (res.statusCode == 500) {
					if (config.ShowMsg) {
						uni.showToast({
							title: '服务器异常请稍后再试',
							icon: 'none'
						})
					}
				} else {
					if (config.ShowMsg) {
						uni.showToast({
							title: '未知错误请稍后重试',
							icon: 'none'
						})
					}
				}
			},
			fail: (res) => {
				if (config.ShowMsg&&res.msg) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
				if (config.ShowLoading) {
					uni.hideLoading();
				}
				if(res.errMsg == 'request:fail'){
					uni.showToast({
						title: '当前无网络，请连接网络后重启应用',
						icon: 'none'
					})
				}
				reject(res);
			}
		})
	})
}

let defaultConfig={
	ShowLoading : true,
	ShowMsg : true,
	getAllData : false
}
	
export default {
	Get: function(url, data = {},config={...defaultConfig}) {
		return requestMethods(url,data,"Get",config)
	},
	Post: function(url, data = {},config={...defaultConfig}) {
		return requestMethods(url,data,"POST",config)
	},
	Put: function(url, data = {},config={...defaultConfig}) {
		return requestMethods(url,data,"PUT",config)
	},
}
